<template>
  <div class="page-container page-footer-btn">
    <van-row>
      <van-col span="24">
        <FortenScan
          ref="scan"
          :propPlaceholder="config.scanTitle"
          :propErrorMsg="config.scanTitle"
          :propTypeArr="config.ruleCode ? [config.ruleCode] :  []"
          @scanCall="handleScanOver"
        />
      </van-col>
    </van-row>


    <van-row>
      <van-cell>
        <FortenList ref="listItem" :propAutoFetchData='false' :propFlagNoPage="true" >
          <template #default="{item}">

            <!--头部-->
            <van-row>
              <van-col span="6">
                <van-checkbox :name="item" v-model="checkBoxList" style="float: left; margin-top: 4px;"></van-checkbox>
                <van-tag round type="primary"  style="float: left; margin-top: 4px;margin-left: 9px;">{{getLeftSpan(item)}}</van-tag>
              </van-col>
              <van-col span="12">{{config.itemHead.center.text}}:{{item[config.itemHead.center.name]}}</van-col>
              <van-col span="6">
                <van-tag round    :style="{float:'left',marginTop: '4px',marginLeft: '9px'  } ">{{flowNodeMap[item.flowStatus].name}}</van-tag>
              </van-col>
            </van-row>

            <!--动态字段-->

            <van-col :span="itemConfig.col" v-for="(itemConfig,index) in config.item"
                     :key="index" >{{itemConfig.text}}:{{item[itemConfig.name]}}</van-col>
          </template>
        </FortenList>
      </van-cell>
    </van-row>

    <van-row type="flex" justify="space-around" class="footer-btn">

      <van-col span="6" v-for="(flowBtn,index) in config.flowBtnList"
               :key="index" >
        <van-button type="primary" @click="flowBtnClick(flowBtn)" block >
          {{flowBtn.text}}
        </van-button>
      </van-col>
      <van-col span="6">
        <van-button type="danger" @click="handleClick" block>
          取消
        </van-button>
      </van-col>
    </van-row>
  </div>
</template>

<script>
/* eslint-disable*/
import { List, Pagination, Popover   } from 'vant'
import auditConfig from '@/util/const/auditConfig.js'

export default {
  title: 'dataview模板',
  name: 'FortenDataviewModel',
  components:{List, Pagination, Popover },
  data() {
    return {
      scan: '',
      config: {},
      activeNames: [],
      checkBoxList: [],
      flowNodeMap: {},
    };
  },
  props: [  'propViewKey'],
  methods: {
    reload() {
      this.scan = ''
      this.$refs.listItem.list = []
      this.$refs.scan.focus()
    },
    //扫描
    handleScanOver(scanValue) {
      let findIndex = this.$refs.listItem.list.findIndex(dataOb => dataOb.busKey == scanValue )
      if (findIndex != -1) {
        this.$dialog.confirm({
          title: '系统提示',
          message: '已添加过该业务单据，是否从列表移除',
        })
          .then(() => {
            this.$refs.listItem.list.splice(findIndex, 1)
            this.scan =  ''
            this.$refs.scan.focus()
          })
          .catch(() => {
            this.scan =  ''
            this.$refs.scan.focus()
          })

        return
      }

      this.utils.loadShow()
      this.$ajax.get(`${this.config.scanAction}?key=bus_key&value=${scanValue}&isScan=1`,{
      }).then(resp => {
        if (resp.code == 0) {
          if (this.config.scanAfter) {
            if (!this.config.scanAfter(resp.data, this)) return
          }
          this.$refs.listItem.pushData(resp.data)
          this.scan =  ''
          this.$refs.scan.focus()
        }
        this.utils.loadHide()
      })
    },

    //按钮操作
    handleClick() {
      this.$store.commit('SET_WORKBENCH_ROUTE', { name: 'dashboard' })
    },
    //流程按钮操作
    flowBtnClick(btn) {
      let params =  []
      let checkList = this.$refs.listItem.getChecks()

      if (!checkList || checkList.length < 1) {
        this.$notify.warning('请选择需要操作的数据')
        return
      }

      this.$dialog.confirm({
        title: '系统提示',
        message: `确认执行[${btn.text}]么，影响数据[${checkList.length}]`,
      })
        .then(() => {
          this.utils.loadShow()
          for (let j = 0; j < checkList.length; j ++) {
            let param = {
              busId: checkList[j].id,
              buttonKey: btn.key,
              flowKey: this.flowKey
            }
            params.push(param)
          }
          let url = this.config.scanAction.substring(0,this.config.scanAction.lastIndexOf('/'))+'/flow_change_list'
          this.$ajax.post(url, params).then(resp => {
            if (resp.code == 0) {
              this.$notify.success(resp.msg)
              this.reload()
            }
            this.utils.loadHide()
          })
        })
        .catch(() => {
        })
    },

    //查询流程节点
    getFlowNode(flowCode) {
      this.$ajax.get(`/flownode/findByFlowCode?flowCode=${flowCode}`).then(resp => {
        if (resp.code == 0) {
          for(let i=0 ; i<resp.data.length ; i++){
            this.flowNodeMap[resp.data[i].value] = resp.data[i]
          }
        }
      })
    },

    getLeftSpan(item) {
      let value = item[this.config.itemHead.left.leftRight]
      if (!value) value ='0/0'
      return value
    }
  },
  created() {
    this.viewKey = this.$route.params.viewKey
    this.flowKey = this.$route.params.flowKey
    if (!this.viewKey) {
      alert('缺少参数【viewKey】')
      return
    }
    if (!this.flowKey) {
      alert('缺少参数【flowKey】')
      return
    }
    this.config = auditConfig[this.viewKey]()
    setTimeout(() => {
      this.$refs.scan.focus()
    }, 500)
    this.getFlowNode('wm_dist')
  }
}
</script>

<style scoped lang="scss">
</style>
